package com.sx.water.modules.sever.impl;

import com.sx.water.datasourcse.DataSourceNames;
import com.sx.water.datasourcse.annnotiaon.DataSource;
import com.sx.water.modules.dao.WaterLineDao;
import com.sx.water.modules.entity.WaterRainBo;
import com.sx.water.modules.sever.WaterLineService;
import com.sx.water.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: chz
 * @Date: 2019/1/11 11:43
 * @Describe:
 */
@Service
public class WaterLineServiceImpl implements WaterLineService {

    @Autowired
    WaterLineDao waterLineDao;

    @Override
    @DataSource(name = DataSourceNames.THIRD)
    public Map getRsvrAndRiverInfo(String time) {

        Map<String, List<Map<String, Object>>> map = new HashMap<>();
        //水库
        WaterRainBo rsvrNow = new WaterRainBo();
        WaterRainBo rsvrOld = new WaterRainBo();
        List<Map<String, Object>> rsvrListNow = new ArrayList<>();
        List<Map<String, Object>> rsvrListOld = new ArrayList<>();

        //河道
        WaterRainBo riverNow = new WaterRainBo();
        WaterRainBo riverOld = new WaterRainBo();
        List<Map<String, Object>> riverListNow = new ArrayList<>();
        List<Map<String, Object>> riverListOld = new ArrayList<>();

        if (StringUtils.hasText(time)) {
            Map<String, String> mapTime = GetTimeByFiveMinit(time);
            //当前数据
            rsvrNow.setStartTime(mapTime.get("now"));
            riverNow.setStartTime(mapTime.get("now"));
            //五分钟之前数据
            rsvrOld.setStartTime(mapTime.get("old"));
            riverOld.setStartTime(mapTime.get("old"));

            rsvrListNow = waterLineDao.selectRsvrRainByTime(rsvrNow);
            rsvrListOld = waterLineDao.selectRsvrRainByTime(rsvrOld);

            riverListNow = waterLineDao.selectRiverRainByTime(riverNow);
            riverListOld = waterLineDao.selectRiverRainByTime(riverOld);
        } else {
            rsvrListNow = waterLineDao.selectRsvrWaterRain(rsvrNow);
            riverListNow = waterLineDao.selectRiverWaterRain(riverNow);
            if (rsvrListNow.size() > 0 && !rsvrListNow.isEmpty()) {
                String times = rsvrListNow.get(0).get("TM").toString();
                Map<String, String> mapTime = GetTimeByFiveMinit(GetParmtoString(times));
                rsvrOld.setStartTime(mapTime.get("old"));
                rsvrListOld = waterLineDao.selectRsvrRainByTime(rsvrOld);

            }
            if (riverListNow.size() > 0 && !riverListNow.isEmpty()) {
                String times = riverListNow.get(0).get("TM").toString();
                Map<String, String> mapTime = GetTimeByFiveMinit(GetParmtoString(times));
                riverOld.setStartTime(mapTime.get("old"));
                riverListOld = waterLineDao.selectRiverRainByTime(riverOld);

            }
        }

        for (int i = 0; i < rsvrListNow.size(); i++) {
            for (int m = 0; m < rsvrListOld.size(); m++) {
                Float now = Float.parseFloat(rsvrListNow.get(i).get("Z").toString());
                Float old = Float.parseFloat(rsvrListOld.get(i).get("Z").toString());
                if (now > old) {
                    rsvrListNow.get(i).put("sign", "+");
                } else if (now < old) {
                    rsvrListNow.get(i).put("sign", "-");
                } else {
                    rsvrListNow.get(i).put("sign", "=");
                }
            }
//            Float max_z = Float.parseFloat(listSn.get(i).get("MAX_Z").toString());
//            Float z = Float.parseFloat(listSn.get(i).get("Z").toString());
//            Float res = z - max_z;
//            if (res > 0) {
//                listSn.get(i).put("err", "+" + df.format(res));
//            } else {
//                listSn.get(i).put("err", df.format(res));
//            }
//            Float ord = z / max_z;
//            listSn.get(i).put("ord",df.format(ord));
            String stcd = rsvrListNow.get(i).get("STCD").toString();
            String stnm = rsvrListNow.get(i).get("STNM").toString();
            rsvrListNow.get(i).put("STNM", stnm.replace(" ", ""));
            rsvrListNow.get(i).put("STCD", stcd.replace(" ", ""));
        }

        for (int i = 0; i < riverListNow.size(); i++) {
            for (int m = 0; m < riverListOld.size(); m++) {
                Float now = Float.parseFloat(riverListNow.get(i).get("Z").toString());
                Float old = Float.parseFloat(rsvrListOld.get(i).get("Z").toString());
                if (now > old) {
                    riverListNow.get(i).put("sign", "+");
                } else if (now < old) {
                    riverListNow.get(i).put("sign", "-");
                } else {
                    riverListNow.get(i).put("sign", "=");
                }
            }
//            Float max_z = Float.parseFloat(riverListNow.get(i).get("MAX_Z").toString());
//            Float z = Float.parseFloat(riverListNow.get(i).get("Z").toString());
//            Float res = z - max_z;
//            if (res > 0) {
//                listHn.get(i).put("err", "+" + df.format(res));
//            } else {
//                listHn.get(i).put("err", df.format(res));
//            }
//            Float ord = z / max_z;
//            listHn.get(i).put("ord", df.format(ord));
            String stcd = riverListNow.get(i).get("STCD").toString();
            String stnm = riverListNow.get(i).get("STNM").toString();
            riverListNow.get(i).put("STNM", stnm.replace(" ", ""));
            riverListNow.get(i).put("STCD", stcd.replace(" ", ""));
        }

        map.put("1", rsvrListNow);
        map.put("2", riverListNow);
        return null;
    }

    @Override
    @DataSource(name = DataSourceNames.THIRD)
    public Map<String, List<String>> selectWaterByTime(String startTime, String endTime) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, List<String>> map = new HashMap<>();
        Date datestr = DateUtils.stringToDate(startTime, DateUtils.DATE_TIME_PATTERN_2);
        Date datend = DateUtils.stringToDate(endTime, DateUtils.DATE_TIME_PATTERN_2);
        WaterRainBo waterRainBo = new WaterRainBo();
        waterRainBo.setStartTime(df.format(datestr));
        waterRainBo.setEndTime(df.format(datend));
        List<String> rsvrList = waterLineDao.selectRsvrTimeListByTime(waterRainBo);
        List<String> riverList = waterLineDao.selectRiverTimeListByTime(waterRainBo);
        map.put("rsvr", rsvrList);
        map.put("river", riverList);
        return map;
    }

    @Override
    @DataSource(name = DataSourceNames.THIRD)
    public Map<String,List<Map<String, Object>>> selectWaterByTimeAndStation(String stationId, String starTime, int num) {

        Map<String,List<Map<String, Object>>> result = new HashMap<>();
        Map<String, String> map = new HashMap<>();
        if (num != 0) {
            map = GetTimeBy24Hour(starTime, num);
        } else {
            map = GetTimeBy24Hour(starTime, 24);
        }
        WaterRainBo waterRainBo = new WaterRainBo();
        waterRainBo.setStartTime(map.get("old"));
        waterRainBo.setEndTime(map.get("now"));
        waterRainBo.setStationId(stationId);
        List<Map<String, Object>> mapRsvrList = waterLineDao.selectRsvrByTimeAndStation(waterRainBo);
        List<Map<String, Object>> mapRiverList = waterLineDao.selectRiverByTimeAndStation(waterRainBo);
        if (mapRsvrList.size() > 0) {
            for (int i = 0; i < mapRsvrList.size(); i++) {
                String stcd = mapRsvrList.get(i).get("STCD").toString();
                String stnm = mapRsvrList.get(i).get("STNM").toString();
                mapRsvrList.get(i).put("STNM", stnm.replace(" ", ""));
                mapRsvrList.get(i).put("STCD", stcd.replace(" ", ""));
            }
        }

        if (mapRiverList.size() > 0) {
            for (int i = 0; i < mapRiverList.size(); i++) {
                String stcd = mapRiverList.get(i).get("STCD").toString();
                String stnm = mapRiverList.get(i).get("STNM").toString();
                mapRiverList.get(i).put("STNM", stnm.replace(" ", ""));
                mapRiverList.get(i).put("STCD", stcd.replace(" ", ""));
            }
        }
        result.put("rsvr",mapRsvrList);
        result.put("river",mapRiverList);
        return result;
    }

    /**
     * yymmddHHmm
     * 获取间隔5分钟的时间
     *
     * @param time
     * @return
     */
    private Map<String, String> GetTimeByFiveMinit(String time) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.000");
        Map<String, String> map = new HashMap<>();

        Date date = DateUtils.stringToDate(time, DateUtils.DATE_TIME_PATTERN_2);
        //现在/传入时间
        String nowTime = df.format(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, -5);
        String oldTime = df.format(calendar.getTime());
        map.put("now", nowTime);
        map.put("old", oldTime);

        return map;
    }

    /**
     * yymmddHHmm
     * 获取间隔 num 小时的时间
     * @param time
     * @return
     */
    private Map<String,String> GetTimeBy24Hour(String time,int num){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.000");
        Map<String,String> map=new HashMap<>();

        Date date= DateUtils.stringToDate(time,DateUtils.DATE_TIME_PATTERN_2);
        //现在/传入时间
        String nowTime=df.format(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR,-num);
        String oldTime=df.format(calendar.getTime());
        map.put("now",nowTime);
        map.put("old",oldTime);

        return map;
    }

    /**
     * yymmddHHmm
     * 截取字符串
     *
     * @param time
     * @return
     */
    private String GetParmtoString(String time) {
        return time.replace(" ", "").replace("-", "").replace(":", "").substring(2, 12);

    }
}
